home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
59454
/
59454.xpi
/
content
/
SearchEngineWrapper.js
< prev
Wrap
Text File
|
2010-01-15
|
8KB
|
231 lines
BartUniversalSearch.SearchEngineObserver = BartUtils.defineClass(
{
name: "UniversalSearchSearchEngineObserver",
construct: function()
{
this.register();
},
methods:
{
observe: function(subject, topic, verb)
{
if(topic == "browser-search-engine-modified")
{
switch(verb)
{
case "engine-changed":
case "engine-added":
case "engine-removed":
BartUniversalSearch.SearchEngineWrapper._buildSearchEngineList();
break;
}
}
},
register: function()
{
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "browser-search-engine-modified", false);
},
unregister: function()
{
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "browser-search-engine-modified");
}
}
});
BartUniversalSearch.SearchEngineWrapper = BartUtils.defineClass(
{
name: "UniversalSearchEngineWrapper",
construct: function(engine)
{
this.engine = engine;
},
methods:
{
getName: function()
{
return this.engine.name;
},
/**
* Get Icon url of the engine. May be null.
*/
getIconUrl: function(defaultUrl)
{
var result = this.engine.iconURI ? this.engine.iconURI.spec : null;
if(!result)
{
result = (defaultUrl == undefined? null : defaultUrl);
}
return result;
},
/**
* Get a search url for a search term
*/
getSearchUrl: function(searchTerm)
{
var url = this.engine.getSubmission(searchTerm, null).uri.spec;
if(url.search(/ebay.com\//i) != -1)
{
url = "http://rover.ebay.com/rover/1/711-53200-19255-0/1?type=4&campid=5336506940&toolid=10001&mpre="
+ encodeURIComponent("http://search.ebay.com/"+searchTerm);
}
else if(url.search(/amazon.com\//i) != -1)
{
url = "http://www.amazon.com/s/?url=search-alias%3Daps&field-keywords=" + searchTerm + "&tag=ff-ex-us-us-20"
}
return url;
}
},
statics:
{
_removeFromString: function(str, remove)
{
//var pos = str.indexOf(remove);
var pos = str.search(new RegExp(remove, "i"));
if(pos != -1)
{
var pos2 = str.indexOf("&", pos);
if(pos2 != -1)
{
str = str.substring(0, pos) + str.substring(pos2 + 1);
}
else
{
str = str.substring(0, pos);
}
}
return str;
},
getAvailableSearchEngines: function()
{
return BartUniversalSearch.SearchEngineWrapper.availableEngines;
},
/**
* Get engine by name. Return null if not found.
*/
getEngine: function(name, useDefaultIfNotFound)
{
var engines = BartUniversalSearch.SearchEngineWrapper.getAvailableSearchEngines();
if(engines)
{
for(var i = 0; i < engines.length; i++)
{
if(engines[i].getName() == name)
return engines[i];
}
}
return useDefaultIfNotFound ? BartUniversalSearch.SearchEngineWrapper.getDefaultEngine() : null;
},
_buildSearchEngineList: function()
{
var searchService = Components.classes["@mozilla.org/browser/search-service;1"].getService(Components.interfaces.nsIBrowserSearchService);
var engines = searchService.getVisibleEngines({ });
var result = [];
if(engines.length <= 0)
{
// build our engines here
BartUniversalSearch.SearchEngineWrapper._generateCustomEngines(result);
}
else
{
for (var i = 0; i < engines.length; i++)
{
result.push(new BartUniversalSearch.SearchEngineWrapper(engines[i]));
}
}
BartUniversalSearch.SearchEngineWrapper.availableEngines = result;
},
init: function()
{
BartUniversalSearch.SearchEngineWrapper._buildSearchEngineList();
BartUniversalSearch.SearchEngineWrapper.engineObserver = new BartUniversalSearch.SearchEngineObserver();
},
uninit: function()
{
if(BartUniversalSearch.SearchEngineWrapper.engineObserver)
{
BartUniversalSearch.SearchEngineWrapper.engineObserver.unregister();
BartUniversalSearch.SearchEngineWrapper.engineObserver = null;
}
if(BartUniversalSearch.SearchEngineWrapper.availableEngines)
{
BartUniversalSearch.SearchEngineWrapper.availableEngines.splice(0);
}
},
/**
* Create custom engines and save them into engines array.
*/
_generateCustomEngines: function(engines)
{
var customEngine =
{
getName: function() {return "Google";},
getIconUrl: function(defaultUrl) {return defaultUrl;},
getSearchUrl: function(searchTerm)
{
var searchParam = BartUtils.UTF8Encode(searchTerm);
searchParam = searchParam.replace(/%20/g, "+");
return "http://www.google.com/search?q=" + searchParam;
}
};
engines.push(customEngine);
},
/**
* Get the defult search engine. init() must be called before using this method.
* A valid engine will be returned.
*/
getDefaultEngine: function()
{
var searchService = Components.classes["@mozilla.org/browser/search-service;1"].getService(Components.interfaces.nsIBrowserSearchService);
// first we use browser's current engine
var engine = searchService.currentEngine;
if(!engine)
{
// if current engine is empty, use browser default engine
engine = searchService.defaultEngine;
}
if(!engine)
{
// if both current engine and default engine are unavailable, use the first available engine
var availableEngines = BartUniversalSearch.SearchEngineWrapper.getAvailableSearchEngines();
// init() method makes sure that there are always avaliable engines.
engine = availableEngines[0];
}
else
{
engine = new BartUniversalSearch.SearchEngineWrapper(engine);
}
return engine;
}
}
});